package util;

import entity.Point;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.HashMap;

/**
 * @author wuyunbin
 */
public class GaoDeMapUtil {
    private static final double EARTH_RADIUS = 637_1393.00D;
    private static final double RADIAN = Math.PI / 180.00D;
    private static final double HALF = 0.5D;
    private static final String KEY ="b6813fd8ab96911798e88ec97665d75f";
    public static String getAddress(String longitude, String latitude) {
        String url = "https://restapi.amap.com/v3/geocode/regeo?key="+KEY
                +"&location="+longitude + "," + latitude;
        return HttpUtil.httpRequest(url);
    }

    public static String getPoint(String address, String city) {
        String url = "https://restapi.amap.com/v3/geocode/geo?key="+KEY
                +"&address="+address
                +"&city="+city;

        return HttpUtil.httpRequest(url);
    }

    public double getDistance(Point point1, Point point2) {
        double lat1 = Double.parseDouble(point1.getLatitude());
        double lon1 = Double.parseDouble(point1.getLongitude());
        double lat2 = Double.parseDouble(point2.getLatitude());
        double lon2 = Double.parseDouble(point2.getLongitude());
        double x, y, a, b, distance;

        lat1 *= RADIAN;
        lat2 *= RADIAN;
        x = lat1 - lat2;
        y = lon1 - lon2;
        y *= RADIAN;
        a = Math.sin(x * HALF);
        b = Math.sin(y * HALF);
        distance = EARTH_RADIUS * Math.asin(Math.sqrt(a * a + Math.cos(lat1) * Math.cos(lat2) * b * b)) / HALF;

        return distance;
    }
}
